
# vim:syntax=python
from os.path import join as pjoin, basename as pbasename
import sys
from numpy.distutils.misc_util import get_numpy_include_dirs, get_pkg_info

from numscons import GetNumpyEnvironment
from numscons import CheckF77Clib

env = GetNumpyEnvironment(ARGUMENTS)

env.Tool('f2py')

if sys.platform=='win32':
#        define_macros.append(('NOINFINITIES',None))
#        define_macros.append(('NONANS',None))
    env.PrependUnique(CPPDEFINES = '_USE_MATH_DEFINES')

config = env.NumpyConfigure(custom_tests = {'CheckF77Clib' : CheckF77Clib})
if not config.CheckF77Clib():
    raise RuntimeError("Could not get C/F77 runtime information")
config.CheckF77Mangling()
config.Finish()

env.PrependUnique(CPPPATH=[get_numpy_include_dirs(), env["PYEXTCPPPATH"]])
def build_lib(name, ext, libname = None):
    """ext should be .f or .c"""
    if not libname:
        libname = name
    src = env.Glob(pjoin(name, '*%s' % ext))
    assert len(src) > 0
    env.DistutilsStaticExtLibrary(libname, source = src)

# C libraries
build_lib('c_misc', '.c', 'sc_c_misc')
build_lib('cephes', '.c', 'sc_cephes')

# F libraries
# XXX: handle no opt flags for mach
build_lib('mach', '.f', 'sc_mach')
build_lib('toms', '.f', 'sc_toms')
build_lib('amos', '.f', 'sc_amos')
build_lib('cdflib', '.f', 'sc_cdf')
build_lib('specfun', '.f', 'sc_specfunlib')

math_info = get_pkg_info("npymath")
env.MergeFlags(math_info.cflags())
env.MergeFlags(math_info.libs())
env.PrependUnique(LIBPATH = ['.'])

# orthogonal_eval extension
env.NumpyPythonExtension('orthogonal_eval', source = 'orthogonal_eval.c')

# lambertw extension
env.NumpyPythonExtension('lambertw', source = 'lambertw.c')

# Cephes extension
src = ['_cephesmodule.c', 'amos_wrappers.c', 'specfun_wrappers.c', \
       'toms_wrappers.c','cdf_wrappers.c','ufunc_extras.c']

env.NumpyPythonExtension('_cephes', 
                         source = src, 
                         LIBS = ['sc_amos', 'sc_toms', 'sc_c_misc', 'sc_cephes', 'sc_mach',\
                                 'sc_cdf', 'sc_specfunlib']) 

# Specfun extension
env.Prepend(LIBS = ['sc_specfunlib'])
env.NumpyPythonExtension('specfun', source = 'specfun.pyf',
                         F2PYOPTIONS = ["--no-wrap-functions"])
